package com.fetnet.smsalarm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	static Notification notification;
    static NotificationManager notificationManager;
   
    private RadioGroup myradiogroup;
    private RadioGroup myradiogroup2;
    
    static boolean vibrate1_on = true;
    static String KeyWord1 = null;
    SeekBar soundBar = null;
    int volume1 = 5;
    
    static boolean vibrate2_on = true;
    static String KeyWord2 = null;
    SeekBar soundBar2 = null;
    int volume2 = 5;
    String alarm = null;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
        notificationManager=(NotificationManager)super.getSystemService(Context.NOTIFICATION_SERVICE); 
        //Listen for button clicks
		soundBar=(SeekBar)findViewById(R.id.seekBar1);
		soundBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			
		}
		public void onStartTrackingTouch(SeekBar seekBar) {
			
		}
		public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
			volume1 = progress;  //get volume that user set
		}
		});
		findview1();
		setlistener1();
        Button button_save = (Button)findViewById(R.id.button02);
        button_save.setOnClickListener(Cfgsave);
        Button button_load = (Button)findViewById(R.id.button03);
        button_load.setOnClickListener(Cfgload);
        
    	soundBar2=(SeekBar)findViewById(R.id.seekBar2);
		soundBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			
		}
		public void onStartTrackingTouch(SeekBar seekBar) {
			
		}
		public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
			volume2 = progress;  //get volume that user set
		}
		});
		findview2();
		setlistener2();
		Button button_save2 = (Button)findViewById(R.id.button05);
        button_save2.setOnClickListener(Cfgsave2);
        Button button_load2 = (Button)findViewById(R.id.button06);
        button_load2.setOnClickListener(Cfgload2);
        Button button = (Button)findViewById(R.id.button04);
        button.setOnClickListener(listenSms);
	}
    
	private void findview1()
	{
	myradiogroup=(RadioGroup)findViewById(R.id.radiogroup);
	}
	private void setlistener1()
	{
	myradiogroup.setOnCheckedChangeListener(changeradio);
	}
	private RadioGroup.OnCheckedChangeListener changeradio=new RadioGroup.OnCheckedChangeListener()
	{
	public void onCheckedChanged(RadioGroup group, int checkedId)
	{
	switch(checkedId)
	{
	case R.id.radioButton1:
	vibrate1_on = true;
	//Toast.makeText(MainActivity.this,"震動啓動",Toast.LENGTH_LONG).show();
	break;
	case R.id.radioButton2:
	vibrate1_on = false;
	//Toast.makeText(MainActivity.this,"震動關閉",Toast.LENGTH_LONG).show();
	break;
	default:
	Toast.makeText(MainActivity.this,"",Toast.LENGTH_LONG).show();
	}
	}

	};
	
    private OnClickListener Cfgsave = new OnClickListener() {
    	public void onClick(View v) {
 
    		 EditText fieldfilter = (EditText)findViewById(R.id.EditText01);
    		 if(fieldfilter.getText().toString().length() != 0) {
    		    KeyWord1 = fieldfilter.getText().toString();
    		 }
    		 try {
    				File myFile = new File("/sdcard/smsalarm1.cfg");
    				myFile.createNewFile();
    				FileOutputStream fOut = new FileOutputStream(myFile);
    				OutputStreamWriter myOutWriter = 
    										new OutputStreamWriter(fOut);
    				String txtData = KeyWord1+","+volume1+","+vibrate1_on;
    				myOutWriter.append(txtData);
    				myOutWriter.close();
    				fOut.close();
    				Toast.makeText(getBaseContext(),
    						"第一組設定已儲存",
    						Toast.LENGTH_SHORT).show();
    			} catch (Exception e) {
    				Toast.makeText(getBaseContext(), e.getMessage(),
    						Toast.LENGTH_SHORT).show();
    			}

    	}
    };
    
    private OnClickListener Cfgload = new OnClickListener() {
    	public void onClick(View v) {
 
    		 try {
    				File myFile = new File("/sdcard/smsalarm1.cfg");
    				FileInputStream fIn = new FileInputStream(myFile);
    				BufferedReader myReader = new BufferedReader(
    						new InputStreamReader(fIn));
    				String aDataRow = "";
    				String aBuffer = "";
    				while ((aDataRow = myReader.readLine()) != null) {
    					aBuffer += aDataRow + "\n";
    				}
    				String[] txtData = aBuffer.split(",");
    				KeyWord1 = txtData[0];
    				EditText fieldfilter = (EditText)findViewById(R.id.EditText01);
    				fieldfilter.setText(KeyWord1);
    				volume1 = Integer.valueOf(txtData[1]);
 
    				
    				if(txtData[2].contains("true")){
    					vibrate1_on = true;
    					//myradiogroup.check(R.id.radioButton1);
    					((RadioButton) findViewById(R.id.radioButton1)).setChecked(true);
    				} else {
    					vibrate1_on = false;
    					//myradiogroup.check(R.id.radioButton2);
    					((RadioButton) findViewById(R.id.radioButton2)).setChecked(true);
    				}
    				myReader.close();
    				soundBar.setProgress(volume1);
    				
    				Toast.makeText(getBaseContext(),
    						"第一組設定已載入",
    						Toast.LENGTH_SHORT).show();
    				
    			} catch (Exception e) {
    				Toast.makeText(getBaseContext(), e.getMessage(),
    						Toast.LENGTH_SHORT).show();
    			}

    	}
    };
    
    private void findview2()
	{
	myradiogroup2=(RadioGroup)findViewById(R.id.radiogroup2);
	}
	private void setlistener2()
	{
	myradiogroup2.setOnCheckedChangeListener(changeradio2);
	}
	private RadioGroup.OnCheckedChangeListener changeradio2=new RadioGroup.OnCheckedChangeListener()
	{
	public void onCheckedChanged(RadioGroup group, int checkedId)
	{
	switch(checkedId)
	{
	case R.id.radioButton3:
	vibrate2_on = true;
	//Toast.makeText(MainActivity.this,"震動啓動",Toast.LENGTH_LONG).show();
	break;
	case R.id.radioButton4:
	vibrate2_on = false;
	//Toast.makeText(MainActivity.this,"震動關閉",Toast.LENGTH_LONG).show();
	break;
	default:
	Toast.makeText(MainActivity.this,"",Toast.LENGTH_LONG).show();
	}
	}

	};
	
    private OnClickListener Cfgsave2 = new OnClickListener() {
    	public void onClick(View v) {
 
    		 EditText fieldfilter = (EditText)findViewById(R.id.EditText05);
    		 if(fieldfilter.getText().toString().length() != 0) {
    		    KeyWord2 = fieldfilter.getText().toString();
    		 }
    		 try {
    				File myFile = new File("/sdcard/smsalarm2.cfg");
    				myFile.createNewFile();
    				FileOutputStream fOut = new FileOutputStream(myFile);
    				OutputStreamWriter myOutWriter = 
    										new OutputStreamWriter(fOut);
    				String txtData = KeyWord2+","+volume2+","+vibrate2_on;
    				myOutWriter.append(txtData);
    				myOutWriter.close();
    				fOut.close();
    				Toast.makeText(getBaseContext(),
    						"第二組設定已儲存",
    						Toast.LENGTH_SHORT).show();
    			} catch (Exception e) {
    				Toast.makeText(getBaseContext(), e.getMessage(),
    						Toast.LENGTH_SHORT).show();
    			}

    	}
    };
    
    private OnClickListener Cfgload2 = new OnClickListener() {
    	public void onClick(View v) {
 
    		 try {
    				File myFile = new File("/sdcard/smsalarm2.cfg");
    				FileInputStream fIn = new FileInputStream(myFile);
    				BufferedReader myReader = new BufferedReader(
    						new InputStreamReader(fIn));
    				String aDataRow = "";
    				String aBuffer = "";
    				while ((aDataRow = myReader.readLine()) != null) {
    					aBuffer += aDataRow + "\n";
    				}
    				String[] txtData = aBuffer.split(",");
    				KeyWord2 = txtData[0];
    				EditText fieldfilter = (EditText)findViewById(R.id.EditText05);
    				fieldfilter.setText(KeyWord2);
    				volume2 = Integer.valueOf(txtData[1]);
 
    				
    				if(txtData[2].contains("true")){
    					vibrate2_on = true;
    					//myradiogroup.check(R.id.radioButton1);
    					((RadioButton) findViewById(R.id.radioButton3)).setChecked(true);
    				} else {
    					vibrate2_on = false;
    					//myradiogroup.check(R.id.radioButton2);
    					((RadioButton) findViewById(R.id.radioButton4)).setChecked(true);
    				}
    				myReader.close();
    				soundBar2.setProgress(volume2);
    				
    				Toast.makeText(getBaseContext(),
    						"第二組設定已載入",
    						Toast.LENGTH_SHORT).show();
    				
    			} catch (Exception e) {
    				Toast.makeText(getBaseContext(), e.getMessage(),
    						Toast.LENGTH_SHORT).show();
    			}

    	}
    };
    
    private OnClickListener listenSms = new OnClickListener() {
    	public void onClick(View v) {
    		Intent intent = new Intent(MainActivity.this, TextMessageReceiver.class);
    		startService(intent);
    	}
    };
    
    @Override
    public void onBackPressed() {
        Intent backtoHome = new Intent(Intent.ACTION_MAIN);
        backtoHome.addCategory(Intent.CATEGORY_HOME);
        backtoHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(backtoHome);
    }
}
